package com.avocado.configuration.controller;



import com.avocado.commons.model.http.BaseResponse;
import com.avocado.configuration.dto.DataDictionaryAddDto;
import com.avocado.configuration.dto.DataDictionaryEditDto;
import com.avocado.configuration.service.DataDictionaryService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.Principal;


@Controller
@RequestMapping("/dataDictionary/*")
public class DataDictionaryController {


    private final DataDictionaryService dataDictionaryService;

    public DataDictionaryController(DataDictionaryService dataDictionaryService) {
        this.dataDictionaryService = dataDictionaryService;
    }


    @PreAuthorize("hasAnyAuthority('DATA_DICTIONARY_ADD')")
    @RequestMapping("/add")
    public @ResponseBody BaseResponse add(@RequestBody @Validated DataDictionaryAddDto dto, Principal user) throws Exception {

        long id=dataDictionaryService.add(dto,user.getName());

        return BaseResponse.builder().data(id).status(1).message("操作成功!").build();
    }
    @PreAuthorize("hasAnyAuthority('DATA_DICTIONARY_UPDATE')")
    @RequestMapping("/update")
    public @ResponseBody BaseResponse update(@RequestBody @Validated  DataDictionaryEditDto dto, Principal user) throws Exception {


        dataDictionaryService.update(dto,user.getName());


        return BaseResponse.builder().status(1).message("操作成功!").build();
    }
    @PreAuthorize("hasAnyAuthority('DATA_DICTIONARY_FIND_ALL')")
    @RequestMapping("/findAll")
    public @ResponseBody BaseResponse findAll(Principal user)  {

        return BaseResponse.builder().status(1).data(dataDictionaryService.findAll()).build();
    }

    @PreAuthorize("hasAnyAuthority('DATA_DICTIONARY_DELETE_BY_ID')")
    @RequestMapping("/deleteById")
    public @ResponseBody BaseResponse deleteById(Long id,String key) {

        int status=dataDictionaryService.deleteById(id,key)==Boolean.TRUE?1:0;

        return BaseResponse.builder().status(status).message("操作成功!").build();
    }



}
